#include "../apue.h"
#include <sys/wait.h>

int main(void)
{
    pid_t pid;
    if ((pid = fork()) < 0)
            err_sys("fork error");
    else if (pid == 0) {
        if ((pid = fork()) < 0)
            err_sys("fork error");
        else if (pid > 0)
        {
            sleep(1);
            printf("first child(%d) exit.\n", getpid());
            exit(0);
        }

        printf("current second child(%d) parent pid = %d\n", getpid(), getppid());
        sleep(2);
        printf("second child(%d), parent pid = %d\n",getpid(), getppid());
        exit(0);
    }

    if (waitpid(pid, NULL, 0) != pid)
        err_sys("waitpid error");
    else
        printf("original parent(%d), waitpid %d.\n", getpid(), pid);

    exit(0);
}

